package com.jcl.recruit.console.logic;

import com.jcl.core.utils.EncodeMd;
import com.jcl.recruit.console.ConsoleUserAccessDao;
import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ConsoleUserAccessLogic

 * @Author yyliu
 * @Date 2018/9/14 9:03
 * @Version 1.0
 **/
@Component
public class ConsoleUserAccessLogic {

    @Autowired
    private ConsoleUserAccessDao consoleUserAccessDao;


    public ConsoleUserAccess addExternalUserByUsername(String username,
                                                       String password,
                                                       String nickname,
                                                       String category,
                                                       Integer status) {
        ConsoleUserAccess userAccess = new ConsoleUserAccess();
        userAccess.setNickname(nickname);
        userAccess.setCategory(category);
        userAccess.setStatus(status);
        try {
            userAccess.setPassword(EncodeMd.EncodeByMd5(username));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        userAccess.setPassword(password);
        userAccess.setCreateTime(new Date());
        userAccess.setUpdateTime(new Date());
        add(userAccess);
        return userAccess;
    }



    public int changeUseStatusToUnused(String externalKey, String category) {

        return changeStatusesToSetting(UserExternalLoginConstant.USER_ACCESS_STATUS_UNUSED,
                externalKey, category, UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
    }

    public int changeUseStatusToInvalid(String externalKey, String category) {
        return changeStatusesToSetting(UserExternalLoginConstant.USER_ACCESS_STATUS_INVALID,
                externalKey, category, UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
    }

    public ConsoleUserAccess findWechatOneByUserId(Integer userId) {
        return consoleUserAccessDao.findOneByUserId(userId, UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT);
    }

    public List<ConsoleUserAccess> findList(String category, Integer... status) {
        return consoleUserAccessDao.findList(category, status);
    }

    public List<ConsoleUserAccess> findListByExternalKey(String externalKey,
                                                         String category,
                                                         Integer status) {
        return consoleUserAccessDao.findListByExternalKey(externalKey, category, status);
    }

    public List<ConsoleUserAccess> findListByUsername(String username,
                                                      String category,
                                                      Integer status) {
        return consoleUserAccessDao.findListByUsername(username, category, status);
    }

    public ConsoleUserAccess findOneByExternalKeyAndCode(String externalKey,
                                                         String corpCode) {
        return consoleUserAccessDao.findOneByExternalKeyAndCode(externalKey, corpCode);
    }

    public ConsoleUserAccess findOneByUserId(Integer userId, String category) {
        return consoleUserAccessDao.findOneByUserId(userId, category);
    }

    public ConsoleUserAccess findOne(Integer id) {
        return consoleUserAccessDao.findOne(id);
    }

    public int add(ConsoleUserAccess access) {
        return consoleUserAccessDao.add(access);
    }

    public int updateAccess(ConsoleUserAccess access) {
        return consoleUserAccessDao.update(access);
    }

    public int update(String externalKey,
                      Integer userId,
                      String category) {
        return consoleUserAccessDao.updateUserId(externalKey, userId, category);
    }

    public int changeStatusesToSetting(Integer setting, String externalKey, String category, Integer... statuses) {
        return consoleUserAccessDao.changeStatusesToSetting(setting, externalKey, category, statuses);
    }

}
